<template>
    <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      :title="'文章详情-'+article.title"
    ></van-nav-bar>
    <!-- /导航栏 -->
    <!-- 加载中 loading -->
    <van-loading v-if="loading" class="article-loading"/>
    <!-- /加载中 loading -->
    <div class="error" v-if="is404">
      <p>页面被外星人吃掉了</p>
      <van-button @click="$router.back()">返回</van-button>
       <van-button @click="$router.push('/home')">返回主页</van-button>
    </div>
    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{article.title}}</h3>
      <author class="author" :author="article">
        <van-button
          round
          size="small"
          type="info"
          @click="hclickFollow"
        >{{article.is_followed? '取消':'关注'}}</van-button>
      </author>
      <div class="content">
        <div>{{article.content}}</div>
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
        round size="small"
        hairline type="primary"
        plain
        :icon="article.attitude ===1? 'good-job':'good-job-o'"
        @click="hZan">
        {{article.attitude===1? '取消赞':'点赞'}}</van-button>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
        round size="small"
        hairline
        type="danger"
        plain icon="delete"
        @click="hLike">
        {{article.attitude===0? '不喜欢':'喜欢'}}</van-button>
      </div>
      <!-- 文章评论 -->
        <article-comment :articleId="articleId"></article-comment>
    </div>
    </div>
</template>
<script>
import { getDetail } from '../../api/article.js'
import { unFollowed, followed, nozan, zan, like, nolike } from '../../api/user.js'
import Author from '../../components/author.vue'
import ArticleComment from './comment'
// import { getarticleId } from '../../api/comment.js'
export default {
  name: 'ArticleIndex',
  data () {
    return {
      loading: false, // 控制加载中的loading状态
      article: {}, // 当前文章
      isFollowed: false, // 关注状态
      is404: false, // 文章是否存在
      articleId: '' // 文章id
    }
  },
  components: {
    Author,
    ArticleComment
  },
  created () {
    this.articleId = this.$route.params.id
    this.loadDetail()
  },
  methods: {
    // 喜欢和不喜欢
    async hLike () {
      try {
        if (this.article.attitude === 0) {
          await like(this.article.art_id.toString())
          this.article.attitude = -1
        } else {
          await nolike(this.article.art_id.toString())
          this.article.attitude = 0
        }
        this.$toast.success('喜欢成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('不喜欢失败')
      }
    },
    // 点赞/取消赞
    async hZan () {
      try {
        if (this.article.attitude === 1) {
          await nozan(this.article.art_id.toString())
        } else {
          await zan(this.article.art_id.toString())
        }
        this.$toast.success('点赞成功')
        this.article.attitude = this.article.attitude * -1
      } catch (err) {
        console.log(err)
        this.$toast.fail('点赞失败')
      }
    },
    // 取关/关注
    async hclickFollow () {
      const { is_followed: isFollowed, aut_id: autId } = this.article
      try {
        if (isFollowed) {
          // 取关
          const res = await unFollowed(autId)
          console.log(res)
          this.$toast.success('取关成功')
          this.article.is_followed = false
        } else {
          // 关注
          const res = await followed(autId)
          console.log(res)
          this.$toast.success('关注成功')
          this.article.is_followed = true
        }
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    },
    // 页面404
    async loadDetail () {
      try {
        this.is404 = false
        this.loading = true
        const id = this.$route.params.id
        const res = await getDetail(id)
        console.log(res)
        this.article = res.data.data
        this.loading = false
      } catch (err) {
        this.loading = false
        console.dir('1', err)
        // 判断本次请求是不是404
        if (err.response.status === 404) {
          this.is404 = true
        }
      }
    }
  }
}
</script>

<style scoped lang='less'>
.article-container{
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error{
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan{
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size:14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img{
      max-width:100%;
      background: #f9f9f9;
    }
  }
}
</style>
